﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DrCarDrive.DBService
{
    class PlaceService
    {
        private DrcardriveDB db;

        public PlaceService()
        {
            db = new DrcardriveDB();
        }

        public int save(tbl_places place)
        {
            DateTime currentDateTime = DateTime.Now;
            
            place.INSERT_DATETIME = currentDateTime;
            place.UPDATE_DATETIME = currentDateTime;

            if (place != null)
            {
                db.Places.Add(place);
            }
            return db.SaveChanges();
        }

        internal int update(tbl_places place)
        {
            DateTime currentDateTime = DateTime.Now;
            place.UPDATE_DATETIME = currentDateTime;

            if (place != null)
            {
                var original = db.Places.SingleOrDefault(p => p.ID.Equals(place.ID));
                db.Entry(original).CurrentValues.SetValues(place);
            }
            return db.SaveChanges();
        }

        public List<tbl_places> viewAll()
        {
            return db.Places.ToList();
        }

        internal tbl_places viewByDriveIDLastRecord(string driveID)
        {
            return db.Places.Where(p => p.FKY_DRIVE.Equals(driveID)).OrderByDescending(p => p.UPDATE_DATETIME).FirstOrDefault(); 
        }

        internal tbl_places viewByPlaceID(string placeID)
        {
            return db.Places.Where(p => p.ID.Equals(placeID)).FirstOrDefault();
        }
    }
}
